package com.sww.pj.sys.service.impl;

import java.util.List;

import com.sww.pj.sys.pojo.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sww.pj.common.exception.ServiceException;
import com.sww.pj.common.pojo.PageObject;
import com.sww.pj.sys.dao.SwwLogDao;
import com.sww.pj.sys.pojo.SwwLog;
import com.sww.pj.sys.service.SwwLogService;

@Service
public class SwwLogServiceImpl implements SwwLogService {
	@Autowired
	private SwwLogDao swwLogDao;


	@Override
	public Log doLogParams(Integer id) {
		Log row = swwLogDao.doLogParams(id);
		return row;
	}


	@Override
	public void saveObject(SwwLog entity) {
		swwLogDao.insertObject(entity);

	}


	@Override
	@RequiresPermissions("sww:log:delete")
	public int deleteObjects(Integer... ids) {
		if(ids==null||ids.length==0)
			throw new IllegalArgumentException("请输入id的值");
		int rows=swwLogDao.deleteObjects(ids);
		if(rows==0)
			throw new ServiceException("记录可能已经不存在");
		return rows;
	}

	@Override
	@RequiresPermissions("sww:log:find")
	public PageObject<SwwLog> findPageObjects(String username, Long pageCurrent) {
		//验证参数的有效性
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("当前页码值无效");//e.message="当前页码值无效"
		//统计总记录数并进行校验
		long rowCount=swwLogDao.getRowCount(username);
		if(rowCount==0)
			throw new ServiceException("没有找到对应结果");
		//查询当前页数据
		int  pageSize=5;//(这个值也可以从页面传递到服务端)
		long startIndex=(pageCurrent-1)*pageSize;
		List<SwwLog> records=swwLogDao.findPageObjects(username, startIndex, pageSize);
		return new PageObject<>(rowCount, records,pageSize, pageCurrent);
	}
}
